﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EC_DataBase;

namespace EasyCheque.User
{
    public partial class PersonalInfo : System.Web.UI.Page
    {
        // 一些在这里要用到的字符串。
        private string m_str_EditPersonalInfo = "编辑信息";
        private string m_str_SaveEditing = "保存";
        //private string m_str_CancelEditing = "取消";
        //private string m_str_Male = "男";
        //private string m_str_Female = "女";

        private void readDataFromDB()
        {
            Person_Info infoHere;
            infoHere = OperateDB.Get_Person_Info(Convert.ToInt32(Session["uid"]));

            if (infoHere != null)
            {
                TextBox_NAME.Text = infoHere.name;
                TextBox_SEX.Text = infoHere.sex;
                TextBox_ENAME.Text = infoHere.e_name;
                TextBox_BIRTHDAY.Text = infoHere.birthday;
                TextBox_IDCARD.Text = infoHere.id_card;
                TextBox_ADDRESS.Text = infoHere.address;
                TextBox_PHONE.Text = infoHere.phone;
                TextBox_EMAIL.Text = infoHere.email;
                TextBox_CELLPHONE.Text = infoHere.cellphone;
            }
        }

        private void refreshTextBoxes(bool readOnly)
        {
            Label_REQUIRE_ADDRESS.Visible = !readOnly;
            Label_REQUIRE_EMAIL.Visible = !readOnly;

            TextBox_ADDRESS.ReadOnly = readOnly;
            TextBox_PHONE.ReadOnly = readOnly;
            TextBox_CELLPHONE.ReadOnly = readOnly;
            TextBox_EMAIL.ReadOnly = readOnly;

            string theCSSClass = "";
            if (readOnly)
            {
                theCSSClass = "textbox_noedit";
            }
            else
            {
                theCSSClass = "";
            }

            TextBox_ADDRESS.CssClass = theCSSClass;
            TextBox_PHONE.CssClass = theCSSClass;
            TextBox_CELLPHONE.CssClass = theCSSClass;
            TextBox_EMAIL.CssClass = theCSSClass;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            // 界面处理。
            Panel_Calendar.Style["display"] = "none";
            Panel_Calendar.Style["position"] = "absolute";
            refreshTextBoxes(true);

            // 控件内容填充。
            // 从数据库中读出数据。
            readDataFromDB();
            // ~
        }

        protected void Calendar_InPanel_SelectionChanged(object sender, EventArgs e)
        {
            TextBox_BIRTHDAY.Text = Calendar_InPanel.SelectedDate.ToString("yyyy-MM-dd");
        }

        protected void Calendar_InPanel_VisibleMonthChanged(Object sender, MonthChangedEventArgs e)
        {
            Panel_Calendar.Style["display"] = "";
        }

        protected void Button_EditPersonalInfo_Click(object sender, EventArgs e)
        {
            bool readOnly = true;

            if (Button_EditPersonalInfo.Text == m_str_EditPersonalInfo)
            {
                // 用户点击了“编辑信息”。

                Button_EditPersonalInfo.Text = m_str_SaveEditing;
                Button_CancelEditing.Visible = true;
                Button_EditPersonalInfo.CausesValidation = true;
                TableRow_LoginPassword.Visible = true;
                readOnly = false;

                TextBox_PASSWORD.Text = "";
            }
            else
            {
                // 用户点击了“保存”。

                // 检测密码正确性。
                if (!Security.MD5.Exam(TextBox_PASSWORD.Text, System.Text.Encoding.ASCII, OperateDB.GetLoginPwd(Convert.ToInt32(Session["uid"]))))
                {
                    // 检测到密码无效。
                    System.Diagnostics.Debug.WriteLine("Password invalid.");
                    Label_CommitMessage.Text = "密码无效。";
                    return;
                }
                else
                {
                    Label_CommitMessage.Text = "";
                }

                // 更改控件样式。
                Button_EditPersonalInfo.Text = m_str_EditPersonalInfo;
                Button_CancelEditing.Visible = false;
                Button_EditPersonalInfo.CausesValidation = false;
                TableRow_LoginPassword.Visible = false;
                readOnly = true;

                // 保存到数据库中。
                int relectedRows = AccessDB.Update(
                    OperateDB.TN_PERSON_INFO,
                    " ADDRESS = '" + TextBox_ADDRESS.Text.ToString()
                    + "', PHONE = '" + TextBox_PHONE.Text.ToString()
                    + "', CELLPHONE = '" + TextBox_CELLPHONE.Text.ToString()
                    + "', EMAIL = '" + TextBox_EMAIL.Text.ToString() + "' ",
                    " ID = " + Session["uid"]);

                if (relectedRows <= 0)
                {
                    System.Diagnostics.Debug.WriteLine("Error updating.");
                }
                // ~
                //跳转
                Response.Redirect("~/Person/PersonalInfo.aspx");
            }
            refreshTextBoxes(readOnly);
        }

        protected void Button_CancelEditing_Click(object sender, EventArgs e)
        {
            // 控件处理。
            Button_EditPersonalInfo.Text = m_str_EditPersonalInfo;
            Button_CancelEditing.Visible = false;
            TableRow_LoginPassword.Visible = false;
            bool readOnly = true;

            refreshTextBoxes(readOnly);

            // 用户取消了用户编辑。
            // 从数据库中读出原来的数据。
            readDataFromDB();
            // ~
        }
    }
}
